package com.ruoyi.system.service.backend.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.CaseInfo;
import com.ruoyi.system.domain.backend.*;
import com.ruoyi.system.mapper.backend.CaseBackMapper;
import com.ruoyi.system.mapper.backend.StudentBackMapper;
import com.ruoyi.system.service.backend.ICaseBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class CaseBackServiceImpl extends CxBase implements ICaseBackService {
    @Autowired
    private CaseBackMapper caseBackMapper;
    @Autowired
    private StudentBackMapper studentBackMapper;


    /**
     * 查询报警列表
     *
     * @param caseBack 查询信息
     * @return 报警列表
     */
    public List<CaseBackInfo> selectcaseback(CaseBack caseBack){
        if(!checkNull(caseBack.getUserId())&&!checkNull(checkNull(caseBack.getUserUnity()))) {
            startPage();
            List<CaseBackInfo> caseBackInfos = caseBackMapper.selectcaseback(caseBack);
            for(CaseBackInfo caseBackInfo :caseBackInfos){
                caseBackInfo.setCaseFirstTime(StampToString(Long.valueOf(caseBackInfo.getCaseFirstTime())));
                caseBackInfo.setCaseReturnVisitTime(StampToString(Long.valueOf(caseBackInfo.getCaseReturnVisitTime())));
                caseBackInfo.setCaseReviseTime(StampToString(Long.valueOf(caseBackInfo.getCaseReviseTime())));
            }
            return caseBackInfos;
        }
        else{
            return null;
        }
    }
    /**
     * 查询报警详细信息
     *
     * @param caseBack 查询信息
     * @return 报警列表
     */
    public CaseBackInfoPic selectcasebackInfo(CaseBack caseBack){
        if(!checkNull(caseBack.getCaseId())) {
            CaseBackInfoPic caseBackInfoPic = caseBackMapper.selectcasebackInfo(caseBack);
            List<String> casePic = caseBackMapper.caseinfoBackPic(caseBack);
            caseBackInfoPic.setCaseFirstTime(StampToString(Long.valueOf(caseBackInfoPic.getCaseFirstTime())));
            caseBackInfoPic.setCaseReturnVisitTime(StampToString(Long.valueOf(caseBackInfoPic.getCaseReturnVisitTime())));
            caseBackInfoPic.setCaseReviseTime(StampToString(Long.valueOf(caseBackInfoPic.getCaseReviseTime())));
            if(caseBackInfoPic.getCaseCollage().equals('0')){
                caseBackInfoPic.setCaseCollage("否");
            }
            else{
                caseBackInfoPic.setCaseCollage("是");
            }
            if(casePic.size()>0){
                caseBackInfoPic.setCasePic(casePic);
            }
            return caseBackInfoPic;
        }
        else{
            return null;
        }
    }

    /**
     * 报警状态更新
     *
     * @param caseBackStatus 修改状态信息
     * @return 报警列表
     */
    public int updateCaseBackStatus(CaseBackStatus caseBackStatus){
        if(!checkNull(caseBackStatus.getCaseId())&&!checkNull(caseBackStatus.getCaseStatus())) {
            return caseBackMapper.updateCaseBackStatus(caseBackStatus);
        }
        else{
            return 0;
        }
    }

    /**
     * 案件调配处理
     *
     * @param caseBackDeployment 修改状态信息
     * @return 报警列表
     */
    public int updateCaseBackDeployment(CaseBackDeployment caseBackDeployment){
        if(!checkNull(caseBackDeployment.getCaseId())) {
            SysBackUsrInfo sysBackUsrInfo = new SysBackUsrInfo();
            sysBackUsrInfo.setUserName(caseBackDeployment.getPoliceId());
            SysBackUsrInfo sysBackUsrInfoPolice = studentBackMapper.selectuserbackinfo(sysBackUsrInfo);
            caseBackDeployment.setPoliceName(sysBackUsrInfoPolice.getUserName());
            caseBackDeployment.setPoliceUnity(sysBackUsrInfoPolice.getUserUnity());
            if(caseBackDeployment.getCaseCollege() == 1){
                sysBackUsrInfo.setUserName(caseBackDeployment.getUserId());
                SysBackUsrInfo sysBackUsrInfoUser = studentBackMapper.selectuserbackinfo(sysBackUsrInfo);
                caseBackDeployment.setUserName(sysBackUsrInfoUser.getUserName());
                caseBackDeployment.setUserUnity(sysBackUsrInfoUser.getUserUnity());
            }
            return caseBackMapper.updateCaseBackDeployment(caseBackDeployment);
        }
        else{
            return 0;
        }
    }
    /**
     * 回访处理
     *
     * @param caseInfo 回访信息
     * @return
     */
    public int updateCaseReturnVisit(CaseInfo caseInfo){
        if(!checkNull(caseInfo.getCaseId())&&!checkNull(caseInfo.getCaseReturnVisitContent())&&!checkNull(caseInfo.getCaseReturnVisitId())){
            SysBackUsrInfo sysBackUsrInfo = new SysBackUsrInfo();
            sysBackUsrInfo.setUserName(caseInfo.getCaseReturnVisitId());
            SysBackUsrInfo sysBackUsrInfoPolice = studentBackMapper.selectuserbackinfo(sysBackUsrInfo);
            caseInfo.setCaseReturnVisitName(sysBackUsrInfoPolice.getUserName());
            caseInfo.setCaseReturnVisitUnity(sysBackUsrInfoPolice.getUserUnity());
            caseInfo.setCaseReturnVisitTime(getStamp());
            return caseBackMapper.updateCaseReturnVisit(caseInfo);
        }
        return 0;
    }
    /**
     * 查询封面统计信息
     *
     * @param caseBack 查询信息
     * @return 报警列表
     */
    public CaseBackStatistics selectcaseInfoback(CaseBack caseBack) throws ParseException {
        if(!checkNull(caseBack.getUserId())&&!checkNull(caseBack.getUserUnity())){
            CaseBackStatistics caseBackStatistics = new CaseBackStatistics();
            List<CaseBackInfo> caseBackInfos = caseBackMapper.selectcaseInfoback(caseBack);
            for(CaseBackInfo caseBackInfo : caseBackInfos){
                switch (caseBackInfo.getCaseStatus()){
                    case "0":
                        caseBackStatistics.setCaseNone(caseBackStatistics.getCaseNone()+1);
                        break;
                    case "1":
                        caseBackStatistics.setCaseIn(caseBackStatistics.getCaseIn()+1);
                        break;
                    case "3":
                        caseBackStatistics.setCaseNoneVisit(caseBackStatistics.getCaseNoneVisit()+1);
                        break;
                    case "4":
                        caseBackStatistics.setCaseFinish(caseBackStatistics.getCaseFinish()+1);
                        break;
                }
            }
            Date date = (new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date(getStamp() * 1000))));
            Calendar datelist = Calendar.getInstance();
            datelist.setTime(date);
            datelist.add(Calendar.DAY_OF_MONTH, -6);
            CaseBackWeek caseBackWeek1 = new CaseBackWeek();
            CaseBackWeek caseBackWeek2 = new CaseBackWeek();
            List<CaseBackWeek> caseBackWeeks1 = new ArrayList<>();
            List<CaseBackWeek> caseBackWeeks2 = new ArrayList<>();
            Long frontdate,afterdate;
            for(int dateinfo = 0;dateinfo<7;dateinfo++){
                frontdate = datelist.getTime().getTime()/1000;
                datelist.add(Calendar.DAY_OF_MONTH, 1);
                afterdate = datelist.getTime().getTime()/1000;
                caseBack.setFrontDate(frontdate);
                caseBack.setAfterDate(afterdate);
                caseBack.setCaseType(5);
                caseBackWeek1.setCaseData(new SimpleDateFormat("yyyy-MM-dd").format(new Date(frontdate * 1000)));;
                caseBackWeek2.setCaseData(new SimpleDateFormat("yyyy-MM-dd").format(new Date(frontdate * 1000)));
                caseBackWeek1.setCaseNumber(caseBackMapper.selectCaseDayDate(caseBack));
                caseBack.setCaseType(4);
                caseBackWeek2.setCaseNumber(caseBackMapper.selectCaseDayDate(caseBack));
                caseBackWeeks1.add(caseBackWeek1);
                caseBackWeeks2.add(caseBackWeek2);
                caseBackWeek1 = new CaseBackWeek();
                caseBackWeek2 = new CaseBackWeek();
            }
            caseBackStatistics.setWeekCase(caseBackWeeks1);
            caseBackStatistics.setWeekCaseVisit(caseBackWeeks2);
            return caseBackStatistics;
        }
        return null;
    }

}
